home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xpat2-1.000 / xpat2-1 / xpat2-1.04 / makecards / src / combine.c < prev    next >
C/C++ Source or Header  |  1994-03-31  |  5KB  |  148 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <assert.h>
  6. #include <stdarg.h>
  7.  
  8. #ifndef max
  9. #define max(a, b)     ((a) > (b) ? (a) : (b))
  10. #define min(a, b)     ((a) < (b) ? (a) : (b))
  11. #endif
  12.  
  13. #include <X11/Xlib.h>
  14. #include <X11/Xutil.h>
  15. #include <X11/Xos.h>
  16. #include <X11/Intrinsic.h>
  17. #include <X11/StringDefs.h>
  18. #include <X11/Xaw/Paned.h>
  19. #include <X11/Xaw/Command.h>
  20.  
  21. #include <xpm.h>
  22.  
  23. static const char
  24.     n_ace[] = "Ace", n_deuce[] = "Deuce", n_three[] = "Three",
  25.     n_four[] = "Four", n_five[] = "Five", n_six[] = "Six", n_seven[] = "Seven",
  26.     n_eight[] = "Eight", n_nine[] = "Nine", n_ten[] = "Ten", n_jack[] = "Jack",
  27.     n_queen[] = "Queen", n_king[] = "King",
  28.     n_diamonds[] = "Diamonds", n_hearts[] = "Hearts", n_spades[] = "Spades",
  29.     n_clubs[] = "Clubs";
  30.  
  31. const char *US_rank_name[] = {
  32.     n_ace, n_deuce, n_three, n_four, n_five, n_six, n_seven, n_eight, n_nine,
  33.     n_ten, n_jack, n_queen, n_king };
  34. const char *US_suit_name[] = {
  35.     n_clubs, n_spades, n_hearts, n_diamonds };
  36.  
  37.  
  38. Display    *dpy;
  39. int    screen;
  40. Window    table;
  41.  
  42. static int read_xpmfile(char *name, Pixmap *image, Pixmap *clip, int *w, int *h) {
  43.     XpmAttributes attribs;
  44.     attribs.valuemask = 0;
  45.     if (XpmReadFileToPixmap(dpy, table, name, image, clip, &attribs) != XpmSuccess)
  46.     return 0;
  47.     if (w) *w = attribs.width;
  48.     if (h) *h = attribs.height;
  49.     return 1;
  50. }
  51.  
  52. static GC gc, g1, clipgc;
  53. static Pixmap pics[12], clip[12], big, clp;
  54.  
  55. static void make_GCs(void) {
  56.     XGCValues gcv;
  57.     long gcflags;
  58.     
  59.     gcv.foreground = BlackPixel(dpy, screen);
  60.     gcv.background = WhitePixel(dpy, screen);
  61.     gcv.graphics_exposures = False;
  62.     gcflags = GCForeground | GCBackground | GCGraphicsExposures;
  63.     gc = XCreateGC(dpy, big, gcflags, &gcv);
  64.     g1 = XCreateGC(dpy, clp, gcflags, &gcv);
  65.     gcv.foreground = 1;
  66.     gcv.background = 0;
  67.     clipgc = XCreateGC(dpy, clp, gcflags, &gcv);
  68. }
  69.  
  70. static void action(Widget unused, XtPointer a, XtPointer b) {
  71.     int s, r, w, h, i;
  72.     for (r = 10; r < 13; ++r)
  73.     for (s = 0; s < 4; ++s) {
  74.         char name[40];
  75.         i = 4 * (r-10) + s;
  76.         sprintf(name, "%s.%s.pic.xpm", US_rank_name[r], US_suit_name[s]);
  77.         if (!read_xpmfile(name, pics+i, clip+i, &w, &h)) {
  78.         fprintf(stderr, "cannot read %s\n", name);
  79.         exit(1);
  80.         }
  81.     }
  82.     big = XCreatePixmap(dpy, RootWindow(dpy, screen), 4 * w, 3 * h, DefaultDepth(dpy, screen));
  83.     clp = XCreatePixmap(dpy, RootWindow(dpy, screen), 4 * w, 3 * h, 1);
  84.     make_GCs();
  85.     XFillRectangle(dpy, clp, clipgc, 0, 0, 4*w, 3*h);
  86.  
  87.     for (r = 10; r < 13; ++r)
  88.     for (s = 0; s < 4; ++s) {
  89.         i = 4 * (r-10) + s;
  90.         XCopyArea(dpy, pics[i], big, gc, 0, 0, w, h, s * w, (r-10) * h);
  91.         if (clip[i])
  92.         XCopyArea(dpy, clip[i], clp, g1, 0, 0, w, h, s * w, (r-10) * h);
  93.     }
  94.     if (XpmWriteFileFromPixmap(dpy, "Pictures.xpm", big, clp, NULL) != XpmSuccess) {
  95.         fprintf(stderr, "error writing xpm file Pictures.xpm\n");
  96.     exit(1);
  97.     }
  98.     exit(0);
  99. }
  100.  
  101. static void action2(Widget unused, XtPointer a, XtPointer b) {
  102.     int s, r, w, h, i;
  103.     if (!read_xpmfile("Pictures.xpm", &big, &clp, &w, &h)) {
  104.     fprintf(stderr, "cannot read Pictures.xpm\n");
  105.     exit(1);
  106.     }
  107.     w /= 4;
  108.     h /= 3;
  109.     make_GCs();
  110.  
  111.     for (r = 10; r < 13; ++r)
  112.     for (s = 0; s < 4; ++s) {
  113.         char name[40];
  114.         i = 4 * (r-10) + s;
  115.         sprintf(name, "%s.%s.pic.xpm", US_rank_name[r], US_suit_name[s]);
  116.         pics[i] = XCreatePixmap(dpy, RootWindow(dpy, screen), w, h, DefaultDepth(dpy, screen));
  117.         clip[i] = XCreatePixmap(dpy, RootWindow(dpy, screen), w, h, 1);
  118.         XCopyArea(dpy, big, pics[i], gc, s * w, (r-10) * h, w, h, 0, 0);
  119.         XCopyArea(dpy, clp, clip[i], g1, s * w, (r-10) * h, w, h, 0, 0);
  120.         if (XpmWriteFileFromPixmap(dpy, name, pics[i], clip[i], NULL) != XpmSuccess) {
  121.         fprintf(stderr, "error writing file %s\n", name);
  122.         exit(1);
  123.         }
  124.     }
  125.     exit(0);
  126. }
  127.  
  128.  
  129. int main(int argc, char *argv[]) {
  130.     XtAppContext app_con;
  131.     Widget toplevel, paned, text1, text2;
  132.  
  133.     toplevel = XtAppInitialize(&app_con, "Combine", NULL, 0, &argc, argv, NULL, NULL, 0);
  134.     paned = XtCreateManagedWidget("paned", panedWidgetClass, toplevel, NULL, 0);
  135.     text1 = XtCreateManagedWidget("Click to combine", commandWidgetClass, paned, NULL, 0);
  136.     text2 = XtCreateManagedWidget("Click to separate", commandWidgetClass, paned, NULL, 0);
  137.     XtAddCallback(text1, XtNcallback, action, NULL);
  138.     XtAddCallback(text2, XtNcallback, action2, NULL);
  139.  
  140.     XtRealizeWidget(toplevel);
  141.     dpy = XtDisplay(toplevel);
  142.     screen = DefaultScreen(dpy);
  143.     table  = XtWindow(paned);
  144.  
  145.     XtAppMainLoop(app_con);    /* does not return */
  146.     return 0;
  147. }
  148.